import 'package:flutter/material.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:flutter_axrjc/widget/CachePage.dart';
import 'package:flutter_axrjc/widget/dialog.dart';
import 'package:get/get.dart';

import 'logic.dart';

class CheckLogPage extends StatelessWidget {
  const CheckLogPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<CheckLogLogic>();
    final state = Get.find<CheckLogLogic>().state;

    return CachePage(
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Padding(
          padding: const EdgeInsets.symmetric(vertical: 5),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Padding(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
                  child: IntrinsicHeight(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: Row(
                            children: [
                              Expanded(
                                child: GestureDetector(
                                  onTap: () {
                                    showCustomDatePicker(onConfirm: (t, s) {
                                      state.startTime.value = t;
                                      state.endTime.value =
                                          t.add(const Duration(days: 30));
                                    });
                                  },
                                  child: Container(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 10, vertical: 8),
                                      decoration: BoxDecoration(
                                          color: Colors.white,
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          border: Border.all(
                                              color:
                                                  Colors.grey.withAlpha(100))),
                                      child: Center(
                                        child: Obx(() {
                                          return Text(
                                            "${Util.date2String(state.startTime.value, pattern: "yyyy-MM-dd")}",
                                          );
                                        }),
                                      )),
                                ),
                              ),
                              const Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Text("至"),
                              ),
                              Expanded(
                                child: GestureDetector(
                                  onTap: () {
                                    showCustomDatePicker(
                                        minDateTime: state.startTime.value,
                                        maxDateTime: state.startTime.value
                                            .add(const Duration(days: 30)),
                                        onConfirm: (t, s) {
                                          state.endTime.value = t;
                                        },
                                        dateFormat: 'yyyy-M月-d日 H时:m分');
                                  },
                                  child: Container(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 10, vertical: 8),
                                      decoration: BoxDecoration(
                                          color: Colors.white,
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          border: Border.all(
                                              color:
                                                  Colors.grey.withAlpha(100))),
                                      child: Center(
                                        child: Obx(() {
                                          return Text(
                                            "${Util.date2String(state.endTime.value, pattern: "yyyy-MM-dd")}",
                                          );
                                        }),
                                      )),
                                ),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(
                          width: 10,
                        ),
                        InkWell(
                          onTap: () {
                            logic.getLog();
                          },
                          child: Container(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 15),
                              decoration: BoxDecoration(
                                  color: orangeColor,
                                  borderRadius: BorderRadius.circular(5)),
                              child: const Center(
                                child: Row(
                                  children: [
                                    Icon(
                                      Icons.search,
                                      color: Colors.white,
                                    ),
                                    Text(
                                      "查询",
                                      style: TextStyle(color: Colors.white),
                                    ),
                                  ],
                                ),
                              )),
                        )
                      ],
                    ),
                  ),
                ),
                Obx(() {
                  if (state.list.isEmpty) {
                    return SizedBox(
                        height: Get.height / 2,
                        child: const Center(
                            child: Text(
                          "暂无数据",
                          style: TextStyle(color: Colors.grey),
                        )));
                  }
                  return ListView.builder(
                    padding: const EdgeInsets.all(10),
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: state.list.length,
                    itemBuilder: (BuildContext context, int index) {
                      var bean = state.list[index];
                      return ExpansionTile(
                        leading: Text("${bean.key}"),
                        expandedAlignment: Alignment.centerLeft,
                        title: Container(),
                        children: List.generate(bean.value.length, (index1) {
                          var bean1 = bean.value[index1];
                          return Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Container(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 10, vertical: 10),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10),
                                  boxShadow: [
                                    BoxShadow(
                                        color: Colors.grey.withAlpha(20),
                                        spreadRadius: 5,
                                        blurRadius: 5)
                                  ]),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    Util.date2String(
                                            Util.string2Date(
                                                "${bean1.detectionStartTime}")!,
                                            pattern: "HH:mm") ??
                                        '',
                                    style: const TextStyle(color: Colors.grey),
                                  ),
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        children: [
                                          const Text("亮度"),
                                          const SizedBox(
                                            width: 5,
                                          ),
                                          Text(
                                            "${bean1.detectionAverageValue}lux",
                                            style: const TextStyle(
                                                color: orangeColor),
                                          ),
                                        ],
                                      ),
                                      // Row(
                                      //   children: [
                                      //     const Text("色温"),
                                      //     const SizedBox(
                                      //       width: 5,
                                      //     ),
                                      //     Text(
                                      //       "${bean1.colorTemp ?? 0}",
                                      //       style: const TextStyle(
                                      //           color: orangeColor),
                                      //     ),
                                      //   ],
                                      // )
                                    ],
                                  ),
                                  const SizedBox(
                                    height: 10,
                                  ),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        "${bean1.mobileBrand}-${bean1.mobileModel}",
                                        style:
                                            const TextStyle(color: Colors.grey),
                                      ),
                                      Text(
                                        "检测耗时 ${getTimes("${bean1.detectionStartTime}", "${bean1.detectionEndTime}")}s",
                                        style:
                                            const TextStyle(color: Colors.grey),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        }),
                      );
                    },
                  );
                })
              ],
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          shape: const CircleBorder(),
          onPressed: () {
            Get.toNamed(MainRoutes.reportMonth);
          },
          child: const Center(
              child: Text(
            "报告",
            style: TextStyle(color: orangeColor),
          )),
        ),
      ),
    );
  }

  int getTimes(String s, String t) {
    return (Util.string2Date(s)!.difference(Util.string2Date(t)!).inSeconds)
        .abs();
  }
}
